package com.zcc.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端请求的参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if (username != null && password != null) {
            if (username.equals("admin")&& password.equals("root")) {
                //将登录信息储存到Session当中，这样其他页面也能拿到
                req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
                //跳转页面
                resp.sendRedirect("/sys/success.jsp");
                System.out.println("登录成功");
            }
            else {
                resp.sendRedirect("/error.jsp");
                System.out.println("用户名或密码错误");
            }
        }
        else {
            System.out.println("不能为空");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
